//
//  Date+Extension.swift
//  mmda
//
//  Created by 李陆涛 on 2018/5/26.
//  Copyright © 2018年 mmda. All rights reserved.
//

import Foundation

extension Date {
    
    func leftTimeStringWithFormat(_ format:String) -> String? {
        
        let timeInterval = -self.timeIntervalSinceNow
        
        if timeInterval < 60 {
            return "刚刚"
        }
        if timeInterval < 60 * 60 * 1 {
            
            return "\(Int(timeInterval / 60)) 分钟前"
        }
        
        if timeInterval < 60 * 60 * 24 {
            
            return "\(Int(timeInterval / 60 / 60)) 小时前"
        }
        
        return string(format)
    }
    
    func string(_ format:String) -> String? {
        
        let dateformatter = DateFormatter()
        
        dateformatter.dateFormat = format
        
        return dateformatter.string(from: self)
    }
    
    static func date(_ string:String, _ format: String) -> Date? {
        
        let dateformatter = DateFormatter()
        
        dateformatter.dateFormat = format
        
        return dateformatter.date(from: string)
    }
    
}
